package com.chen.controller;

import com.chen.pojo.*;
import com.chen.service.BuildingService;
import com.chen.service.TeacherWithBuildingService;
import com.chen.utils.Constants;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class BuildingController {
    @Autowired
    private BuildingService buildingService;
    @Autowired
    private TeacherWithBuildingService teacherWithBuildingService;
    @RequestMapping("/getBuildingList.html")
    public ModelAndView getBuildingList(Building building, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        ModelAndView modelAndView = new ModelAndView();
        PageHelper.startPage(pageNum, Constants.PAGESIZE);
        List<Building> list = buildingService.getBuildingList(building);
        PageInfo<Building> page = new PageInfo<Building>(list);
        if (list.size() == 0) {
            modelAndView.addObject(Constants.BUILDING, list);
        } else {
            modelAndView.addObject(Constants.BUILDING, page);
        }
        modelAndView.setViewName("/WEB-INF/pages/admin/buildingList");
        return modelAndView;
    }

    //根据id查询宿舍楼信息
    @RequestMapping("to_updateBuilding.html")
    public String toUpdateBuilding(Integer id, HttpServletRequest request) {
        System.out.println(id);
        Building building = buildingService.getBuildingById(id);
        request.getSession().setAttribute("building", building);
        return "/WEB-INF/pages/admin/updateBuilding";
    }

    //修改宿舍楼信息
    @RequestMapping("updateBuilding.html")
    public String updateBuilding(Building building, HttpServletRequest request) {
        System.out.println(building);
        buildingService.updateBuilding(building);
        request.setAttribute("bs", "修改宿舍楼信息成功");
        return "redirect:to_updateBuilding.html";
    }
    //根据楼id查询老师
    @RequestMapping("getTeacherByBuilding.html")
    public String getTeacherByBuilding(Integer id,HttpServletRequest request){
        try{
            //根据id查询有权限的老师
            List<Teacher> teachers1=buildingService.getTeacherByBuilding(id);
            //根据id查询没有权限的老师列表
            List<Teacher> teachers2=buildingService.getNotTeacherByBuilding(id);
            request.setAttribute("teachers1", teachers1);
            request.setAttribute("teachers2", teachers2);
            request.setAttribute("bid",id);
            System.out.println(teachers2);
            return "/WEB-INF/pages/admin/teacherByBuilding";
        }catch (Exception e){
            e.printStackTrace();
            return "defeade";
        }
    }
    @RequestMapping("addTeacherWithBuiding.html")
    @ResponseBody
    public String  addTeacherWithBuiding(TeacherWithBuilding teacherWithBuilding, HttpServletRequest request){
        teacherWithBuildingService.addWith(teacherWithBuilding);
        return "success";
    }
    //删除老师的权限
    @RequestMapping("deleteTeacherWithBuiding.html")
    @ResponseBody
    public String  deleteTeacherWithBuilding(TeacherWithBuilding teacherWithBuilding){
        try {
            teacherWithBuildingService.deleteWith(teacherWithBuilding);
            return "success";
        }catch (Exception e){
            e.printStackTrace();
            return "defeade";
        }

    }

    @RequestMapping("getBuildingListByAjax.html")
    @ResponseBody
    public List<Building>  buildingList(Building building,HttpServletRequest request){
        Integer role = (Integer)request.getSession().getAttribute("USER_TYPE");
//        Teacher teacher =(Teacher) request.getSession().getAttribute("SESSION_USER");
        User teacher = (User) request.getSession().getAttribute("SESSION_USER");
        if(role==1){
            List<Building> buildings = buildingService.getBuildingList(building);
            return buildings;
        }else if (role==2){
            List<Building> buildingsByTeachers = buildingService.getBuildingByTeacherId(teacher.getId());
            return buildingsByTeachers;
        }else{
            return null;
        }
    }

    //根据老师id查询宿舍楼
    @RequestMapping("getBuildingListByTeacher.html")
    public String getBuildingListByTeacher(@RequestParam(defaultValue = "1")Integer pageNum,HttpServletRequest request){
        //得到老师的id;
        Teacher teacher=(Teacher)request.getSession().getAttribute(Constants.SESSION_USER);
        int tid = teacher.getId();
        PageHelper.startPage(pageNum,Constants.PAGESIZE);
        List<Building> buildingsByTeachers = buildingService.getBuildingByTeacherId(tid);
        PageInfo<Building> pageInfo = new PageInfo<Building>(buildingsByTeachers);
        request.setAttribute("pageInfo",pageInfo);
        return "/WEB-INF/pages/teacher/buildingList";
    }
    //去管理宿舍根据老师
    @RequestMapping("to_dormByTeacher.html")
    public String to_dormByTeacher(HttpServletRequest request){
        Teacher teacher=(Teacher)request.getSession().getAttribute(Constants.SESSION_USER);
        int tid = teacher.getId();
        List<Building> buildingsByTeachers = buildingService.getBuildingByTeacherId(tid);
        request.setAttribute("buildingsByTeachers",buildingsByTeachers);
        return "/WEB-INF/pages/teacher/togetDormByBuilding";
    }

}
